<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('Mnews');
		    $this->load->library('pagination');
			$this->lang->load('home', 'vietnamese');
	}
	
	function index($page='')
	{
		$limit = 9;

		$config['base_url'] = site_url().'/news/index/';

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 3;

		$config['num_links'] = 2;

		$config['total_rows'] = $this->Mnews->count_all('tbl_news');
		
		$config['per_page'] = $limit;

		$this->pagination->initialize($config);

    if (empty($page))	$page = 0; //default value
	 $data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());
		$data['title']='Tin tức';
		$data['main'] = 'newspage';
		$data['result'] = $this->Mnews->get_data($page,$limit);
		$this->load->view('template/index',$data);
	}
	function newstype($typeid,$page='')
	{
		
		$limit = 9;

		$config['base_url'] = site_url().'/news/newscat/'.$typeid."/";

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 4;

		$config['num_links'] = 5;

		$config['total_rows'] = $this->Mnews->count_all_type($typeid,'tbl_news');
		
		
		$config['per_page'] = $limit;

		$this->pagination->initialize($config);

    if (empty($page))	$page = 0; //default value
	 	$data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());
		$data['title']='Tin tức';
		$data['main'] = 'newspage_type';
		$data['result'] = $this->Mnews->get_news_type($typeid,$page,$limit);
		$data['result1'] = $this->Mnews->get_nametype($data['result']);
		$data['result2'] = $this->Mnews->get_namecat($data['result']);
		
		$this->load->view('template/index',$data);
	}
	function newscat($typeid,$page='')
	{
		$limit = 9;

		$config['base_url'] = site_url().'/news/newscat/'.$typeid."/";

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 4;

		$config['num_links'] = 5;

		$config['total_rows'] = $this->Mnews->count_all($typeid,'tbl_news');
		
		$config['per_page'] = $limit;

		$this->pagination->initialize($config);

    if (empty($page))	$page = 0; //default value
	 	$data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());
		$data['title']='Tin tức';
		$data['main'] = 'newspage_type';
		$data['result'] = $this->Mnews->get_news_cat($typeid,$page,$limit);
		//$data['result1'] = $this->Mnews->get_nametype($data['result']);
		$data['result2'] = $this->Mnews->get_namecat($data['result']);
		$this->load->view('template/index',$data);
	}
	
	function detail($id,$catid)
	{
		$data['title']='Tin tức';
		$data['main'] = 'news_detail';
		$data['result'] = $this->Mnews->info_detail($id,'tbl_news');
		//$data['result1'] = $this->Mnews->get_nametype($data['result']);
		$data['result2'] = $this->Mnews->get_catnew($catid);
		$data['result3'] = $this->Mnews->get_namecat($data['result2']);
		
		$this->load->view('template/index',$data);
	}
	function search($code="",$keyword="",$need="",$tower="",$type="",$furniture="",$Bedrooms=0,$floors=0,$area=0,$price=0,$tygia="",$pricetype="",$page="")
	{
		$limit = 9;
		// lay bien de search
		$keyword = isset($_REQUEST['code'])?$_REQUEST['code']:$keyword;
		$code = isset($_REQUEST['code'])?$_REQUEST['code']:$code;
		$need = isset($_REQUEST['need'])?$_REQUEST['need']:$need;
		$tower = isset($_REQUEST['tower'])?$_REQUEST['tower']:$tower;
		$Type = isset($_REQUEST['Type'])?$_REQUEST['Type']:$type;
		$Furniture = isset($_REQUEST['Furniture'])?$_REQUEST['Furniture']:$furniture;
		
		$bedrooms = isset($_REQUEST['bedrooms'])?$_REQUEST['bedrooms']:$Bedrooms;
		$bedrooms2 = isset($_REQUEST['bedrooms2'])?$_REQUEST['bedrooms2']:0;
		if($bedrooms > $bedrooms2 and ($bedrooms != 0 or $bedrooms2 != 0))
			$bedrooms=array($bedrooms,$bedrooms2); 
		else if ($bedrooms <= $bedrooms2 and ($bedrooms != 0 or $bedrooms2 != 0))
			$bedrooms=array($bedrooms2,$bedrooms); 
		else
			$bedrooms = $Bedrooms;
			
		$floors = isset($_REQUEST['floors'])?$_REQUEST['floors']:$floors;
		$floors2 = isset($_REQUEST['floors2'])?$_REQUEST['floors2']:0;
		if($floors > $floors2 and ($floors != 0 or $floors2 != 0))
			$floors=array($floors,$floors2); 
		else if ($floors <= $floors2 and ($floors != 0 or $floors2 != 0))
			$floors=array($floors2,$floors); 
		else
			$floors = $floors;
		
		$area = isset($_REQUEST['area'])?$_REQUEST['area']:$area;
		$area2 = isset($_REQUEST['area2'])?$_REQUEST['area2']:0;
		if($area > $area2 and ($area != 0 or $area2 != 0))
			$area=array($area,$area2); 
		else if ($area <= $area2 and ($area != 0 or $area2 != 0))
			$area=array($area2,$area); 
		else
			$area = $area;
		
		$price_from = isset($_REQUEST['price_from'])?$_REQUEST['price_from']:$price;
		$price_to = isset($_REQUEST['price_to'])?$_REQUEST['price_to']:0;
		if($price_from > $price_to and ($price_from != 0 or $price_to != 0))
			$price_from=array($price_from,$price_to); 
		else if ($price_from <= $price_to and ($price_from != 0 or $price_to != 0))
			$price_from=array($price_to,$price_from); 
		else
			$price_from = $price;
			
		$tygia = isset($_REQUEST['tygia'])?$_REQUEST['tygia']:$tygia;
		$pricetype = isset($_REQUEST['pricetype'])?$_REQUEST['pricetype']:$pricetype;
		
		$config['base_url'] = site_url()."/news/search/$code/$keyword/$need/$tower/$Type/$Furniture/$bedrooms/$floors/$price_from/$tygia/$pricetype/$page";

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 4;

		$config['num_links'] = 5;

		$config['total_rows'] = $this->Mnews->count_all_search($code,$keyword,$need,$tower,$Type,$Furniture,$bedrooms,$floors,$area,$price,$tygia,$pricetype);
		
		$config['per_page'] = $limit;

		$this->pagination->initialize($config);

    if (empty($page))	$page = 0; //default value
	 	$data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());
		$data['title']='Tin tức';
		$data['main'] = 'newspage_type';
		$data['result'] = $this->Mnews->newsearch($code,$keyword,$need,$tower,$Type,$Furniture,$bedrooms,$floors,$area,$price,$tygia,$pricetype,$page,$limit);
		
		//$data['result1'] = $this->Mnews->get_nametype($data['result']);
		//$data['result2'] = $this->Mnews->get_namecat($data['result']);
	
		$this->load->view('template/index',$data);
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */